﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace OnlineManagement.Web.webs
{
    public partial class SowLatestHybrization : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int hoggeryID = HoggeryId;
            if(hoggeryID<=0){
                panInfo.Visible = false;
                lbMsg.Text = "没有查询到相关母猪配种信息";
                return;
            }
            repeaterShow.ItemDataBound += new RepeaterItemEventHandler(repeaterShow_ItemDataBound);
            if(!Page.IsPostBack){
                lbMsg.Text = string.Empty;
                string str = Request.QueryString["sowId"] == null ? string.Empty : Request.QueryString["sowId"].ToString();
                string isCycleStr = string.Empty;
                    //Request.QueryString["isCycle"] == null ? string.Empty : Request.QueryString["isCycle"].ToString();

                if (string.IsNullOrEmpty(str) && Request.Cookies.Get("sowId") != null)
                {
                    str = Request.Cookies.Get("sowId").Value;
                }
                if (string.IsNullOrEmpty(isCycleStr) && Request.Cookies.Get("isCycle")!=null) {
                    isCycleStr = Request.Cookies.Get("isCycle").Value;
                }
                Guid sowId; 
                if (!Guid.TryParse(str,out sowId))
                {
                    panInfo.Visible = false;
                    lbMsg.Text = "没有查询到相关母猪配种信息";
                }               
                else {
                    panInfo.Visible = true;
                    bool isCyle = false;
                    bool.TryParse(isCycleStr, out isCyle);
                    BindRepeater(sowId, hoggeryID,isCyle);
                }
            }
        }

        void repeaterShow_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Model.HybridizationRecord record = (Model.HybridizationRecord)e.Item.DataItem;
                Label lbHybridizationCountName = e.Item.FindControl("lbHybridizationCountName") as Label;
                Label comment = e.Item.FindControl("lbComment") as Label;

               lbHybridizationCountName.Text = "第三次配种";
                if(record.HybridizationCount==1){
                    lbHybridizationCountName.Text = "首配";
                }
                else if (record.HybridizationCount == 2)
                {
                    lbHybridizationCountName.Text = "次配";
                }

                if (string.IsNullOrEmpty(record.Comment))
                {
                    comment.Text = Common.PubConstant.EmptyString;
                }
                else {
                    if (record.Comment.Length > 5)
                    {
                        comment.Text =string.Format("{0}...", record.Comment.Substring(0, 5));
                    }
                    else {
                        comment.Text = record.Comment;
                    }
                }


            }
        }

        private void BindRepeater(Guid sowId,int hoggeryId,bool isCyle) {
            List<Model.HybridizationRecord> records = GetSowHybrization(sowId, hoggeryId,isCyle);
            repeaterShow.DataSource = records;
            repeaterShow.DataBind();
        }
        private List<Model.HybridizationRecord> GetSowHybrization(Guid sowId, int hoggeryId,bool isCyle)
        {
            BLL.Dal.HybridizationRecord bllHR = new BLL.Dal.HybridizationRecord();
            List<Model.HybridizationRecord> hyRecords = new List<Model.HybridizationRecord>();
            try
            {
              DataSet ds=  bllHR.GetSowHybrizationRecords(hoggeryId, sowId, isCyle);
              if (ds != null && ds.Tables != null && ds.Tables.Count > 0)
              {
                  for (int i = 0; i < ds.Tables.Count; i++)
                  {
                      DataTable table = ds.Tables[i];
                      Model.HybridizationRecord record = new Model.HybridizationRecord();
                      record.HybridizationCount = i + 1;
                      DateTime dt;
                      DateTime.TryParse(table.Rows[0]["HybridizationDate"].ToString(), out dt);
                      record.HybridizationDate = dt;
                      record.HybridizationPerson = table.Rows[0]["HybridizationPerson"].ToString();
                      record.BoarBitID = table.Rows[0]["BoarBitID"].ToString();
                      record.BoarPigTypeName = table.Rows[0]["PigTypeName"].ToString();
                      record.Comment = table.Rows[0]["Comment"].ToString();

                      if (string.IsNullOrEmpty(record.HybridizationPerson))
                      {
                          record.HybridizationPerson = Common.PubConstant.EmptyString;
                      }
                      hyRecords.Add(record);
                  }
              }
              else {
                  panInfo.Visible = false;
                  lbMsg.Text = "没有查询到相关母猪配种信息";
              }
            }catch(Exception ex){
              log4net.ILog log=  OnlineHoggeryLog.GetLog("OnlineManagement.Web.webs.SowLatestHybrization.GetSowHybrization");
              log.Error(new Com.HoggeryLog() {HoggeryID=hoggeryId,PigId=sowId.ToString() }, ex);
            }
            return hyRecords;
        }

      
    }
}